import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class DashboardDefaultScreen extends StatelessWidget {
  // 入口图标数据
  final List<Map<String, dynamic>> icons = [
    {'name': '商品管理', 'route': '/spu/list'},
    {'name': '分类管理', 'route': '/category'},
    {'name': '规格管理', 'route': null},
    {'name': '设置', 'route': null},
    {'name': '消息', 'route': null},
    {'name': '统计', 'route': null},
    {'name': '帮助', 'route': null},
    {'name': '关于', 'route': null},
  ];

  // 模拟的统计数据
  final Map<String, dynamic> stats = {
    'todayOrders': 25,
    'todayRevenue': 1250.50,
    'avgOrderValue': 50.02,
    'pendingOrders': 3,
    'topSellingProduct': 'T恤',
    'lowSellingProduct': '袜子',
    'lowStockItems': 2,
  };

  DashboardDefaultScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('工作台')),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 上半部分：统计信息（今日概览）
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  GridView.count(
                    crossAxisCount: 4,
                    // 每行 4 项
                    crossAxisSpacing: 10,
                    mainAxisSpacing: 10,
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    childAspectRatio: 1.5,
                    // 保持宽高比
                    children: [
                      _buildStatCard(
                        '今日订单数',
                        '${stats['todayOrders']} 单',
                        Icons.shopping_cart,
                      ),
                      _buildStatCard(
                        '今日收入',
                        '¥${stats['todayRevenue'].toStringAsFixed(2)}',
                        Icons.attach_money,
                      ),
                      _buildStatCard(
                        '平均客单价',
                        '¥${stats['avgOrderValue'].toStringAsFixed(2)}',
                        Icons.monetization_on,
                      ),
                      _buildStatCard(
                        '待处理订单',
                        '${stats['pendingOrders']} 单',
                        Icons.pending_actions,
                      ),
                      _buildStatCard(
                        '销量最高商品',
                        stats['topSellingProduct'],
                        Icons.trending_up,
                      ),
                      _buildStatCard(
                        '销量最低商品',
                        stats['lowSellingProduct'],
                        Icons.trending_down,
                      ),
                      _buildStatCard(
                        '库存预警',
                        '${stats['lowStockItems']} 件',
                        Icons.warning,
                      ),
                    ],
                  ),
                ],
              ),
            ),
            const Divider(),
            // 下半部分：入口图标
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: GridView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 4,
                  crossAxisSpacing: 10,
                  mainAxisSpacing: 10,
                  childAspectRatio: 1,
                ),
                itemCount: icons.length,
                itemBuilder: (context, index) {
                  final iconData = icons[index];
                  return GestureDetector(
                    onTap: () {
                      if (iconData['route'] != null) {
                        context.push(iconData['route']);
                      } else {
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('${iconData['name']} 点击未实现')),
                        );
                      }
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        const Icon(Icons.category, size: 40),
                        const SizedBox(height: 8),
                        Text(iconData['name'], textAlign: TextAlign.center),
                      ],
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 构建统计卡片的小部件
  Widget _buildStatCard(String title, String value, IconData icon) {
    return Container(
      padding: const EdgeInsets.all(4), // 进一步减小内边距
      decoration: BoxDecoration(
        color: Colors.grey[100],
        borderRadius: BorderRadius.circular(8),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min, // 尽量压缩 Column 高度
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            child: FittedBox(
              fit: BoxFit.scaleDown,
              child: Icon(icon, color: Colors.green, size: 18), // 减小图标并动态缩放
            ),
          ),
          Expanded(
            child: FittedBox(
              fit: BoxFit.scaleDown,
              child: Text(
                title,
                style: const TextStyle(fontSize: 8, color: Colors.grey), // 减小字体
                textAlign: TextAlign.center,
              ),
            ),
          ),
          Expanded(
            child: FittedBox(
              fit: BoxFit.scaleDown,
              child: Text(
                value,
                style: const TextStyle(
                  fontSize: 10,
                  fontWeight: FontWeight.bold,
                ), // 减小字体
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
